import HorizontalPlatform
import pygame
image=pygame.image.load("mechanicalpress.jpg")
image.set_colorkey((137,44,75))
import IntervalWorkingDevice

class MechanicalPress(IntervalWorkingDevice.IntervalWorkingDevice, HorizontalPlatform.HorizontalPlatform):
	def __init__(self,pos=(0,0),im=image,intervals=[20,5,14,20],c_interval=0,c_time=1,off=False):
		HorizontalPlatform.HorizontalPlatform.__init__(self,pos,im)
		IntervalWorkingDevice.IntervalWorkingDevice.__init__(self,intervals=intervals,c_interval=c_interval,c_time=c_time,off=off)
	def getHeight(self):
		r=3
		h=HorizontalPlatform.HorizontalPlatform.getHeight(self)
		if self.cur_interval==1:
			h=r+(h-r)*self.cur_time/self.intervals[self.cur_interval]
		elif self.cur_interval==3:
			h=r+(h-r)*(1.0- float(self.cur_time)/self.intervals[self.cur_interval])
		elif self.cur_interval==0:
			h=r
		return h
	def drawingInformation(self):
		A=pygame.Surface((self.getWidth(),self.getHeight()))
		A.fill((137,44,75))
		A.blit(self.image,(0,self.getHeight()-HorizontalPlatform.HorizontalPlatform.getHeight(self)))
		A.set_colorkey((137,44,75))
		return self.pos,A
	def yLanding(self,objecto):
		x2=objecto.intermediate_x(self.lowerSide()-objecto.getHeight()) if (objecto.prev_y<=self.lowerSide()<=objecto.y or objecto.prev_y>=self.lowerSide()>=objecto.y) else objecto.x
		if self.within_x_boundary(x2,objecto.getWidth()) and (self.lowerSide()<=objecto.y+objecto.getHeight()<=self.upperSide()):
			if (not objecto.dead) and not (self.cur_interval==1 and self.pos[1]-HorizontalPlatform.HorizontalPlatform.getHeight(self)>=self.pos[1]-self.getHeight()-objecto.getHeight()):
				objecto.rebound(x2,self.lowerSide()-objecto.getHeight())
			elif (not objecto.dead) and objecto.y_bound:
				objecto.die()
			'''if objecto.canClimb():
				objecto.y_bound=self
				self.boundedList.append(objecto)
				objecto.hangsFromButtom=True'''
	def within_x_boundary(self,ox,ow):
		return HorizontalPlatform.HorizontalPlatform.within_x_boundary(self,ox,ow,k=0.5*0.9999)
	def objectLanding(self,objecto):
		if (not objecto.x_bound):
			self.xLanding(objecto)
		self.yLanding(objecto)
	def clingable(self):
		return False
		
